﻿using System;
using System.IO;
using System.Collections;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace PdfReport
{
    public class PdfReport
    {
        private Document document;
        public string DATE_STRING = "July 2009";
        public string CLIENT_NAME = "Client Name";
        public string REPORT_TYPE = "Total Messages Report";
        public string FILE_NAME = "Test001.pdf";

        private Font h1Font;
        private Font h2Font;
        private Font h3Font;
        private Font bodyFont;
        private Font littleFont;

        public PdfReport(string clientName)
        {
            document = new Document();
            CLIENT_NAME = clientName;
            init();
        }

        public PdfReport(string fileName, string clientName, string reportDate)
        {
            document = new Document();
            CLIENT_NAME = clientName;
            FILE_NAME = fileName;
            DATE_STRING = reportDate;
            init();
        }

        public void init()
        {
            document = new Document();
            h1Font = FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.BOLD);
            h2Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL);
            h3Font = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.BOLD);
            bodyFont = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL);
            littleFont = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL);
            

            try
            {
                //open instance
                PdfWriter.GetInstance(document, new FileStream(FILE_NAME, FileMode.Create));

                string path = Path.Combine(Environment.CurrentDirectory, @"..\bryan800logo.png");

                //metadata
                document.AddTitle(REPORT_TYPE + " (" + DATE_STRING + ") for " + CLIENT_NAME);
                document.AddSubject("Created by Bryan 800 Answering Service");
                document.AddKeywords("Bryan , 800, report, " + REPORT_TYPE + ", " + CLIENT_NAME + ", " + DATE_STRING);
                document.AddCreator("Bryan 800 Answering Service");
                document.AddAuthor("Bryan 800");

                HeaderFooter header = new HeaderFooter(new Phrase("\n" + CLIENT_NAME, h1Font), false);
                
                header.After = new Phrase("\n"+REPORT_TYPE + "\nFor: " + DATE_STRING+"\n\n", h2Font);
                header.Before.Leading = 10.0f;
                header.After.Leading = 16.0f;
                document.Header = header;


                HeaderFooter footer = new HeaderFooter(new Phrase("Generated by Bryan 800 Communications on: " + DateTime.Today.ToString(),littleFont), false);
                document.Footer = footer;
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }
        }

        public void addPage()
        {
            try
            {
                document.Open();
                document.NewPage();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.ToString());
            }
        }

        public void addTable(string header, ArrayList data)
        {
            try
            {
                document.Open();
                Paragraph p = new Paragraph();
                p.Add(new Phrase(header+"\n",h3Font));
                
                for (int i = 0; i < data.Count; i++)
                {
                    p.Add(new Phrase(data[i].ToString()+"\n", bodyFont));
                }
                p.Add("\n");
                document.Add(p);

                

            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }
        }

        public void addTable(string[] header, string[,] data)
        {
            try
            {
                document.Open();
                Table t = new Table(data.GetLength(0));
                t.Border = Table.NO_BORDER;
                for(int i = 0; i < data.GetLength(0); i++)
                {
                    Cell h = new Cell(header[i]);
                    h.Border = Cell.NO_BORDER;
                    t.AddCell(h,0,i);
                    for(int j = 1; j < data.GetLength(1); j++)
                    {
                        Cell c = new Cell(data[j,i]);
                        c.Border = Cell.NO_BORDER;
                        t.AddCell(c, j, i);
                    }
                }
                
                document.Add(t);
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }
        }

        public void print()
        {
            try
            {
                document.Close();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Document Failed To Close");
            }
        }

        static void Main(string[] args)
        {
            PdfReport report = new PdfReport("NewReport.pdf", "Scott & White Health Clinic", "August 2010");
            
            string[] header = new string[5];
            string[,] data = new string[5,5];
            for (int i = 0; i < 5; i++)
            {
                header[i] = "Header("+i+")";
                for(int j=0; j < 5; j++)
                {
                    data[i,j] = "Cell ("+i+","+j+")";
                }
            }

            report.addTable(header, data);
            report.print();
        }
    }
}
